/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agents.interfaceagent;

import BESA.ExceptionBESA;
import BESA.Kernell.Agent.Event.EventBESA;
import BESA.Kernell.Agent.KernellAgentExceptionBESA;
import BESA.Kernell.Agent.StateBESA;
import BESA.Kernell.System.Directory.AgHandlerBESA;
import co.edu.javeriana.ayllu.agents.interfaceagentagent.InterfaceAgent;
import co.edu.javeriana.ayllu.agents.sessionmanageragent.SMA_ReceiveIARequestGuard;
import co.edu.javeriana.ayllu.data.Ayllu_WallMessageData;
import gui.ChatGUI;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A basic infterface Agent with a basic chat interface
 * @author Ayllu
 */
public class IA_SynchCommInterface extends InterfaceAgent {
    String name;
    public IA_SynchCommInterface(String alias, StateBESA state, double passwd) throws KernellAgentExceptionBESA {
        super(alias, state, passwd);
        name = alias;
        ((IA_SynchCommInterfaceState)state).setTheGUI(new ChatGUI(this, name));
    }

    public void sendWallMessage(String messageToSend) {
        try {
            Iterator<String> wallAgentsIte =
                    IA_SynchCommInterface.this.getAdmLocal().searchAidByService("WallService");
            String cmaId;
            if (wallAgentsIte != null) {
                cmaId = wallAgentsIte.next();
            } else {
                cmaId = IA_SynchCommInterface.this.getAdmLocal().getHandlerByAlias("CHAT_CMA").getAgId();
            }
            Ayllu_WallMessageData theData = new Ayllu_WallMessageData(cmaId, "CHAT_CMA", System.currentTimeMillis(), name, messageToSend);
            AgHandlerBESA smahandler = ((IA_SynchCommInterfaceState) state).getSessionManagerHandler();
            EventBESA event = new EventBESA(SMA_ReceiveIARequestGuard.class.getName(), theData);
            smahandler.sendEvent(event);
        } catch (ExceptionBESA ex) {
            Logger.getLogger(IA_SynchCommInterface.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
